package com.hymjweb.demo.hymj.finance_manage.invoice_register.mapper;

import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.hymj.finance_manage.invoice_register.bean.CwInvoiceRegisterPojo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Mapper
@Component
public interface CwInvoiceRegisterMapper {



    class CwInvoiceRegisterMapProvider{

        public String queryExcludeBillByCustomerId(String deliveryCustomerId){
            String sql = "select t.* from b_native_bill t where t.id in (select  t.native_bill_id from cw_invoice_register_detail t where t.cw_invoice_register_id in (select t.id from cw_invoice_register t where t.delivery_customer_id=" + DataTypeUtil.quoteStr(deliveryCustomerId) + "))";
            return sql;
        }


        public String queryInvoiceDetailList(Map param){
            String sql = "select t1.id detail_id, t1.native_bill_id,t1.invoice_code,t2.* from cw_invoice_register_detail t1 left join cw_invoice_register t2 on t1.cw_invoice_register_id = t2.id where 1=1";
            if(param!=null && !param.isEmpty()){
                /**
                 * let keyDeliveryCustomerId = mini.get("keyDeliveryCustomerId");
                 *             let keyBillCode = mini.get("keyBillCode");
                 *             let keyStartDate = mini.get("keyStartDate");
                 *             let keyEndDate = mini.get("keyEndDate");
                 *             let keyInvoiceTypeId = mini.get("keyInvoiceTypeId");
                 *             let keyRateId = mini.get("keyRateId");
                 *
                 *             and t2.delivery_customer_id =''
                 *     and t1.native_bill_id =''
                 *     and t2.create_time
                 *     and t2.invoice_type_id =
                 *         and t2.rate_id =
                 */

                String keyDeliveryCustomerId = DataTypeUtil.toStringObject(param.get("keyDeliveryCustomerId"));
                String keyBillCode = DataTypeUtil.toStringObject(param.get("keyBillCode"));
                String keyInvoiceCode = DataTypeUtil.toStringObject(param.get("keyInvoiceCode"));
                String keyStartDate = DataTypeUtil.toStringObject(param.get("keyStartDate"));
                String keyEndDate = DataTypeUtil.toStringObject(param.get("keyEndDate"));
                Long keyInvoiceTypeId = DataTypeUtil.toLong(param.get("keyInvoiceTypeId"),-1);
                Long keyRateId = DataTypeUtil.toLong(param.get("keyRateId"),-1);

                if(keyDeliveryCustomerId!=null){
                    sql += " and t2.delivery_customer_id = " + DataTypeUtil.quoteStr(keyDeliveryCustomerId);
                }

                if(keyBillCode!=null){
                    sql += " and t1.native_bill_id = " + DataTypeUtil.quoteStr(keyBillCode);
                }

                if(keyInvoiceCode!=null){
                    sql += " and t1.invoice_code = " + DataTypeUtil.quoteStr(keyInvoiceCode);
                }

                if(keyInvoiceTypeId!=-1){
                    sql += " and t2.invoice_type_id = " + keyInvoiceTypeId;
                }

                if(keyRateId!=-1){
                    sql += " and t2.rate_id = " + keyRateId;
                }

                if(keyStartDate!=null){
                    if(keyEndDate!=null){
                        int d=keyStartDate.compareTo(keyEndDate);
                        if(d>0){
                            String tempDate = keyStartDate;
                            keyStartDate = keyEndDate;
                            keyEndDate = tempDate;
                        }
                        sql += " and (date_format(t2.create_time,'%Y-%m-%d') between "+ DataTypeUtil.quoteStr(keyStartDate) +" and " + DataTypeUtil.quoteStr(keyEndDate) + ")";
                    }
                    else{
                        sql += " and date_format(t2.create_time,'%Y-%m-%d') = "+ DataTypeUtil.quoteStr(keyStartDate);
                    }
                }
                else{
                    if(keyEndDate!=null){
                        sql += " and date_format(t2.create_time,'%Y-%m-%d') = "+ DataTypeUtil.quoteStr(keyEndDate);
                    }
                }

            }
            return sql;
        }

        public String insert(CwInvoiceRegisterPojo pojo){
            String sql = new SQL() {{
                INSERT_INTO("cw_invoice_register");
                VALUES("id","#{id}");
                VALUES("delivery_customer_id","#{deliveryCustomerId}");
                VALUES("invoice_amount","#{invoiceAmount}");
                VALUES("open_invoice_company_id","#{openInvoiceCompanyId}");
                VALUES("customer_invoice_id","#{customerInvoiceId}");
                VALUES("invoice_title","#{invoiceTitle}");
                VALUES("taxpayer_identification_number","#{taxpayerIdentificationNumber}");
                VALUES("phone_number","#{phoneNumber}");
                VALUES("invoice_address","#{invoiceAddress}");
                VALUES("open_account_bank_id","#{openAccountBankId}");
                VALUES("bank_account","#{bankAccount}");
                VALUES("invoice_type_id","#{invoiceTypeId}");
                VALUES("rate_id","#{rateId}");
                VALUES("recipient_name","#{recipientName}");
                VALUES("recipient_phone","#{recipientPhone}");
                VALUES("recipient_address","#{recipientAddress}");
                VALUES("create_time", "#{createTime}");
                VALUES("create_user_id", "#{createUserId}");
                VALUES("update_time", "#{updateTime}");
                VALUES("update_user_id", "#{updateUserId}");
            }}.toString();
            return sql;
        }

    }


    @ResultType(Map.class)
    @SelectProvider(type = CwInvoiceRegisterMapProvider.class, method = "queryExcludeBillByCustomerId")
    List<Map<String, Object>>  queryExcludeBillByCustomerId(String deliveryCustomerId);


    @ResultType(Map.class)
    @SelectProvider(type = CwInvoiceRegisterMapProvider.class, method = "queryInvoiceDetailList")
    List<Map<String, Object>> queryInvoiceDetailList(Map param);

    @InsertProvider(type = CwInvoiceRegisterMapProvider.class, method = "insert")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(CwInvoiceRegisterPojo pojo);

}
